package com.reebake.ideal.security.oauth2.client;

import cn.hutool.core.util.IdUtil;
import com.reebake.ideal.cache.util.CacheUtil;
import com.reebake.ideal.security.oauth2.entity.OAuth2BindUser;
import lombok.RequiredArgsConstructor;

import java.util.Map;

@RequiredArgsConstructor
public class OAuth2BindUserRepository {
    private final static String KEY_PREFIX = "oauth2:user-bind:";
    private final Long timeout = 3600000L;

    private String generateKey(String key) {
        return KEY_PREFIX + key;
    }

    public String save(String source, String uuid, Map<String, Object> attributes) {
        OAuth2BindUser oAuth2BindUser = new OAuth2BindUser();
        oAuth2BindUser.setSource(source);
        oAuth2BindUser.setUuid(uuid);
        oAuth2BindUser.setAttributes(attributes);
        String key = IdUtil.fastSimpleUUID();
        CacheUtil.put(generateKey(key), oAuth2BindUser, timeout);
        return key;
    }

    public OAuth2BindUser get(String key) {
        return (OAuth2BindUser) CacheUtil.get(generateKey(key));
    }

    public void remove(String key) {
        CacheUtil.remove(generateKey(key));
    }
}
